// pages/conference/conference.js
import car from '../../../config/api/car' // 引入api接口管理文件
const dayjs = require("./dayjs");
var WxNotificationCenter = require("../../../config/WxNotificationCenter");
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //底部安全区高度
    bottomLift: app.globalData.bottomLift,
    scrollHeight: 0,
    // 会议室集合
    conferenceList: [{
      name: "加载中"
    }],
    conferenceitem: {
      name: "加载中"
    },
    huiyishow: false,
    // 会议室选中
    conferenceIndex: 0,
    openid: '',
    // 日期集合
    dateList: [],
    // 日期选中
    dateIndex: 0,
    timeIndex: 0,
    // 列表
    appointmentList: [],

    initmentlist: [{
        time: '上午',
        status: 0,
        name: '空闲中',
        timeout: false,
        stime: '08:00',
        etime: '12:00'
      },
      {
        time: '下午',
        status: 0,
        name: '空闲中',
        timeout: false,
        stime: '12:00',
        etime: '18:00'
      },
      {
        time: '全天',
        status: 0,
        name: '空闲中',
        timeout: false,
        stime: '08:00',
        etime: '18:00'
      }
    ]
  },

  // 选中日期
  chooseDate(e) {
    let cindex = e.currentTarget.dataset.index;
    if (cindex == this.data.dateIndex) {
      return;
    }
    this.setData({
      dateIndex: cindex,
      timeIndex: 0,
    })
    // this.getRecord();
  },
  // 选中时间
  chooseTime(e) {
    let cindex = e.currentTarget.dataset.index;
    if (cindex == this.data.timeIndex) {
      return;
    }
    this.setData({
      timeIndex: cindex,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


    // } else {
    //   // this.loadData();
    // }

    this.setData({
      openid: app.globalData.openid,
    })


    //处理时间往后七天
    let maplist = []
    for (let index = 0; index < 7; index++) {
      let date = dayjs().add(index, 'day');
      let w = this.weektostring(dayjs(date).format("d"));
      let map = {
        "date": date,
        "format": dayjs(date).format("MM月DD日"),
        "week": w,
      }
      maplist.push(map);
    }
    //处理时间数据appointmentList
    let mentlist = this.data.initmentlist;

    this.setData({
      dateList: maplist,
      initmentlist: mentlist
    })

    // var that = this
    // WxNotificationCenter.addNotification('getRecord', that.getRecord, that)

    // if (options.list) {
    this.getRecord();
    // }
  },
  weektostring(index) {
    index = index * 1;
    switch (index) {
      case 0:
        return "周日";
      case 1:
        return "周一";
      case 2:
        return "周二";
      case 3:
        return "周三";
      case 4:
        return "周四";
      case 5:
        return "周五";
      case 6:
        return "周六";
      default:
        return "";
    }
  },
  loadData() {
    // let vm = this;
    // //加载车辆列表
    // car.list({
    //   "pageNo": 1,
    //   "pageSize": 9999
    // }, function (res) {
    //   let list = res.data.data.records;
    //   vm.setData({
    //     conferenceList: list,
    //     conferenceitem: list[0]
    //   })
    //   vm.getRecord();
    // })
  },
  //获取车辆预约记录
  getRecord() {
    // let vm = this;
    // let stime = dayjs(vm.data.dateList[vm.data.dateIndex].date).format("YYYY-MM-DD 00:00:00");
    // let etime = dayjs(vm.data.dateList[vm.data.dateIndex].date).format("YYYY-MM-DD 23:59:59");
    // car.Reservationlist({
    //   "startTime": stime,
    //   "endTime": etime,
    //   "carId": this.data.conferenceitem.id,
    // }, function (res) {

    //   //处理预约数据
    //   let records = res.data.data.records;

    let mentlist = vm.data.appointmentList;
    let date = dayjs(vm.data.dateList[vm.data.dateIndex].date).format("YYYY-MM-DD");
    let nowtime = new Date();
    for (let index = 0; index < mentlist.length; index++) {
      const item = mentlist[index];
      item.status = 0;
      item.name = '空闲中';
      // //当前循环的时间
      // let cstime = date + " " + item.stime + ":00";
      // let cstimes = new Date(cstime.replace(/-/g, "/"));

      // let cetime = date + " " + item.etime + ":00";
      // let cetimes = new Date(cetime.replace(/-/g, "/"));

      // for (let j = 0; j < records.length; j++) {
      //   const item1 = records[j];
      //   //已经预约的时间
      //   let yystartTime = item1.startTime;
      //   var yystartTimes = new Date(yystartTime.replace(/-/g, "/"));
      //   let yyendTime = item1.endTime;
      //   var yyendTimes = new Date(yyendTime.replace(/-/g, "/"));
      //   if (vm.isDateIntersection(yystartTimes, yyendTimes, cstimes, cetimes)) {
      //     item.status = 1;
      //     item.openId = item1.openId;
      //     item.hyid = item1.id;
      //     item.name = item1.name;
      //   }

      // }
      //判断是否超时
      let datatime = date + " " + item.etime + ":00";
      let lasttime = new Date(datatime.replace(/-/g, "/"));
      item.timeout = lasttime < nowtime;
    }
    vm.setData({
      appointmentList: mentlist
    })
    // });
  },

  //判断两个时间是否有交集   (开始时间) (上午下午时间)
  isDateIntersection(startdate1, enddate1, startdate2, enddate2) {
    if (startdate1 >= startdate2 && startdate1 < enddate2) { //startdate1介于另一个区间之间
      return true;
    }

    if (enddate1 > startdate2 && enddate1 < enddate2) { //enddate1介于另一个区间之间
      return true;
    }

    if (startdate1 <= startdate1 && enddate1 > enddate2) { //startdate1-enddate1的区间大于另一个区间，且另一个区间在startdate1-enddate1之间
      return true;
    }

    return false;
  },
  // 弹窗
  clickshowhy() {
    this.setData({
      huiyishow: true
    })
  },
  //点击预约
  clickyuyue() {
    let item = this.data.appointmentList[this.data.timeIndex];

    if (item.status == 1) {
      wx.showToast({
        title: '车辆已占用!',
        icon: 'none'
      })
      return;
    }
    if (item.timeout) {
      wx.showToast({
        title: '当前时间已过~',
        icon: 'none'
      })
      return;
    }


    item.ferenceitem = this.data.conferenceitem;
    let time = dayjs(this.data.dateList[this.data.dateIndex].date).format("YYYY-MM-DD");
    item.date = time;

    wx.navigateTo({
      url: '/pages/vehicle/add/add?item=' + JSON.stringify(item),
    })

  },

  onCloseshowhy() {
    this.setData({
      huiyishow: false
    })
  },
  //选中会议室
  // onCinfirm(res) {
  //   let detail = res.detail
  //   this.setData({
  //     huiyishow: false,
  //     conferenceIndex: detail.index,
  //     conferenceitem: detail.value
  //   })
  //   this.getRecord();
  // },
  onCancel() {
    this.setData({
      huiyishow: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getScrollHeight();

  },

  getScrollHeight() {
    //如果不加定时器，获取的元素的高度还是没渲染完异步数据前的高度
    let query = wx.createSelectorQuery();
    query.select('.leftDate').boundingClientRect(rect => {
      let height = rect.height; //单位：px
      console.log("rect", rect, height);
      this.setData({
        scrollHeight: height,
        appointmentList: this.data.initmentlist,
      })
    }).exec();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // var that = this
    // WxNotificationCenter.removeNotification('getRecord', that)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})